using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Net;

namespace Com.Rf.Core
{
    public class Page
    {
        public Page(string url, string encoding)
        {
            this.Url = url;
            this.Encoding = encoding;
            Console.WriteLine("visiting：" + url);
        }

        private string Encoding = "GB2312";

        private bool Loaded = false;

        private void Load()
        {
            WebRequest webRequest = WebRequest.Create(Url);
            this.doc = NSoup.NSoupClient.Parse(webRequest.GetResponse().GetResponseStream(), "GB2312");
            Loaded = true;
        }

        public string Url
        { get; set; }

        private NSoup.Nodes.Document doc;

        public NSoup.Select.Elements Select(string query)
        {
            if (!Loaded)
            {
                Load();
            }
            return doc.Select(query);
        }

        public List<string> GetAllLinks()
        {
            if (!Loaded)
            {
                Load();
            }

            List<string> links = new List<string>();
            NSoup.Select.Elements es = doc.Select("a");
            for (int i = 0; i < es.Count; i++)
            {
                string href = es[i].Attr("href").ToLower().Trim();

                string newHref = GetAbsUrl(Url, href);
                if (IsNormalUrl(newHref))
                {
                    if (!string.IsNullOrEmpty(newHref))
                    {
                        while (newHref.EndsWith("#"))
                        {
                            newHref = newHref.Substring(0, newHref.Length - 1);
                        }
                        links.Add(newHref);
                    }
                }
            }

            return links;
        }

        private bool IsNormalUrl(string url)
        {
            if (url.StartsWith("file:"))
                return false;
            if (url.StartsWith("#"))
                return false;
            if (url.StartsWith("javascript:"))
                return false;
            if (url.StartsWith("ftp://"))
                return false;

            return true;
        }

        private string GetAbsUrl(string url, string href)
        {
            url = url.Trim().ToLower();
            href = href.Trim().ToLower();

            Uri uri = new Uri(new Uri(url), href);
            return uri.ToString();
        }
    }
}
